package cn.sdormitory.service.impl;

import cn.sdormitory.basedata.dao.BDormitoryDao;
import cn.sdormitory.basedata.vo.TeacherDorVO;
import cn.sdormitory.common.utils.StringUtils;
import cn.sdormitory.service.AppDormService;
import cn.sdormitory.smartdor.dao.SdAttenceDao;
import cn.sdormitory.smartdor.dao.SdHygieneDao;
import cn.sdormitory.smartdor.dao.SdRepairDao;
import cn.sdormitory.smartdor.dao.SdStudentHygieneDao;
import cn.sdormitory.smartdor.entity.SdHygiene;
import cn.sdormitory.smartdor.entity.SdHygieneStudent;
import cn.sdormitory.smartdor.vo.SdAttenceAppVo;
import cn.sdormitory.smartdor.vo.SdRepairAppVo;
import cn.sdormitory.sys.entity.SysDictDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AppDormServiceImpl implements AppDormService {

    @Autowired
    BDormitoryDao bDormitoryDao;

    @Autowired
    SdAttenceDao sdAttenceDao;

    @Autowired
    SdRepairDao sdRepairDao;

    @Autowired
    SdHygieneDao sdHygieneDao;

    @Autowired
    SdStudentHygieneDao sdStudentHygieneDao;

    @Override
    public List<SdAttenceAppVo> findAttenceList(Map<String, Object> queryParam) {
        String startDate = (String)queryParam.get("startDate");
        String endDate = (String)queryParam.get("endDate");
        String buildingNo = (String)queryParam.get("buildingNo");
        String storey = (String)queryParam.get("storey");
        String dormitoryNo = (String)queryParam.get("dormitoryNo");

        if(StringUtils.isEmpty(startDate) && StringUtils.isEmpty(endDate)) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date();
            startDate = format.format(date);
            endDate = format.format(date);
        }
        return sdAttenceDao.findList(startDate,endDate,buildingNo,storey,dormitoryNo);
    }

    @Override
    public Map<String, Object> findAttencedByStuNo(String stuNo) {
        //正常出勤次数
//        Integer nolmal = sdAttenceDao.findStuAttenceStatus1Count(stuNo);
        //未归寝次数
        Integer notReturn = sdAttenceDao.findStuAttenceStatus2Count(stuNo);
        //晚归次数
        Integer laterReturn = sdAttenceDao.findStuAttenceStatus3Count(stuNo);
        //请假次数
        Integer leave = sdAttenceDao.findStuAttenceStatus4Count(stuNo);
        //未归寝和晚归数据列表
        List<SdAttenceAppVo> stuUnusualAttenceList = sdAttenceDao.findStuUnusualAttenceList(stuNo);
        Map<String, Object> returnMap = new HashMap<>();
//        returnMap.put("normal",nolmal);
        returnMap.put("notReturn",notReturn);
        returnMap.put("laterReturn",laterReturn);
        returnMap.put("leave",leave);
        returnMap.put("stuUnusualAttenceList",stuUnusualAttenceList);
        return returnMap;
    }

    @Override
    public List<SdHygiene> findHygieneList(Map<String, Object> queryParam) {
        String startDate = (String)queryParam.get("startDate");
        String endDate = (String)queryParam.get("endDate");
        String buildingNo = (String)queryParam.get("buildingNo");
        String storey = (String)queryParam.get("storey");
        String dormitoryNo = (String)queryParam.get("dormitoryNo");

        if(StringUtils.isEmpty(startDate) && StringUtils.isEmpty(endDate)) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date();
            startDate = format.format(date);
            endDate = format.format(date);
        }
        return sdHygieneDao.findList(startDate,endDate,buildingNo,storey,dormitoryNo);
    }

    @Override
    public Map<String, Object> findHygieneByStuNo(String stuNo) {
        //优秀次数
        Integer excellent = sdStudentHygieneDao.findStuHygieneExcellentCount(stuNo);
        //良好次数
        Integer good = sdStudentHygieneDao.findStuHygieneGoodCount(stuNo);
        //差次数
        Integer bad = sdStudentHygieneDao.findStuHygieneBadCount(stuNo);
        List<SdHygieneStudent> stuBadHygieneList = sdStudentHygieneDao.findStuBadHygieneList(stuNo);
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("excellent",excellent);
        returnMap.put("good",good);
        returnMap.put("bad",bad);
        returnMap.put("stuBadHygieneList",stuBadHygieneList);
        return returnMap;
    }

    @Override
    public List<SdRepairAppVo> findRepairList(Map<String, Object> queryParam) {
        String startDate = (String)queryParam.get("startDate");
        String endDate = (String)queryParam.get("endDate");
        String buildingNo = (String)queryParam.get("buildingNo");
        String storey = (String)queryParam.get("storey");
        String dormitoryNo = (String)queryParam.get("dormitoryNo");

        if(StringUtils.isEmpty(startDate) && StringUtils.isEmpty(endDate)) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date();
            startDate = format.format(date);
            endDate = format.format(date);
        }
        return sdRepairDao.findList(startDate,endDate,buildingNo,storey,dormitoryNo);
    }

    @Override
    public List<SysDictDetail> findDormBuilding() {
        return bDormitoryDao.findDormBuilding();
    }

    @Override
    public List<SysDictDetail> findDormStorey() {
        return bDormitoryDao.findDormStorey();
    }

    @Override
    public List<String> findDorm(Map<String, Object> param) {
        String buildingNo = (String)param.get("buildingNo");
        String storey = (String)param.get("storey");
        return bDormitoryDao.findDorm(buildingNo,storey);
    }

    @Override
    public List<TeacherDorVO> getTeacherDor(Long teacherId) {
        return bDormitoryDao.getTeacherDor(teacherId);
    }
}
